
function BaseAsset(name,callback)
{
  this.object = null;
  this.loaded = false;
  this.callback = callback;
  this.name = name; 
}

BaseAsset.prototype.isLoaded = function ()
{
  return this.loaded;
}

BaseAsset.prototype.loadedCallback = function ()
{
  this.loaded = true;
  this.callback(this.name);  
}

function CanvasImage(name,src,callback)
{
  BaseAsset.call(this,name,callback);
  this.src = src;
}

CanvasImage.inherit(BaseAsset);

CanvasImage.prototype.load = function ()
{
  this.object = new Image();
  this.object.addEventListener('load',bind(this,this.loadedCallback),true);
  this.object.src = this.src;
}

function CanvasAudio(name,src,callback)
{
  BaseAsset.call(this,name,callback);
  this.src = src;
}

CanvasAudio.inherit(BaseAsset);

CanvasAudio.prototype.load = function ()
{
  this.object = new Audio();
  this.object.addEventListener('canplaythrough',bind(this,this.loadedCallback),true);
  this.object.src = this.src;
  this.object.load();
} 

function BaseAssets()
{
  this.assets = Array();
  this.assetCallback = null;
  this.assetLoaderLocked = false;
}

BaseAssets.prototype.isAssetLoaderLocked = function ()
{
  return this.assetLoaderLocked;
}

BaseAssets.prototype.hasAssetName = function (name)
{
  for(var i in this.assets)
    if(this.assets[i].name == name)
      return true;
      
  return false;
}

BaseAssets.prototype.addImage = function (name,src)
{
  if(this.isAssetLoaderLocked())
    throw new Error('Asset Loader is locked.');
  
  if(this.hasAssetName(name))
    throw new Error('Asset name already in use.');
    
  this.assets.push(new CanvasImage(name,src,bind(this,this.assetLoadedCallback)));
}

BaseAssets.prototype.addAudio = function (name,src)
{
  if(this.isAssetLoaderLocked())
    throw new Error('Asset Loader is locked.');  
  
  if(this.hasAssetName(name))
    throw new Exception('Asset name already in use');
    
  this.assets.push(new CanvasAudio(name,src,bind(this,this.assetLoadedCallback)));  
}

BaseAssets.prototype.getAsset = function (name)
{
  if(!this.hasAssetName(name))
    throw new Error('Unknown Asset');
  
  for(var i in this.assets)
    if(this.assets[i].name == name)
      return this.assets[i].object;
  
  return null;
}

BaseAssets.prototype.assetLoadedCallback = function (name)
{
  for(var i in this.assets)
    if(!this.assets[i].isLoaded())
      return;

  this.assetLoaderLocked = false;
  this.assetCallback();
}

BaseAssets.prototype.loadAssets = function (callback)
{
  if(this.isAssetLoaderLocked())
    throw new Error('Asset Loader is locked.');
      
  this.assetLoaderLocked = true;
  this.assetCallback = callback;
  
  if(this.assets.length == 0)
    this.assetCallback();
  else
    for(var i in this.assets)
      this.assets[i].load();
}

BaseAssets.prototype.clearAssets = function ()
{
  this.assets = Array();
  this.assetCallback = null;
  this.assetLoaderLocked = false;
}

/*
var e = new BaseAssets();

function loaded()
{
  alert('hit');
}

function main()
{
  e.addImage('button_play_mouseOff','Images/title.png');
  e.addImage('button_play_mouseOn','Images/title2.png');
  e.loadAssets(loaded);
}
*/